<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>React 配置代理 | 小马の博客</title><meta name="keywords" content="React 配置代理"><meta name="author" content="Kun Kun"><meta name="copyright" content="Kun Kun"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="React ajax axios 配置代理1、React 中配置代理(proxy)  简单代理:在 package.json 中追加如下配置 :&quot;proxy&quot;:http:&#x2F;&#x2F;localhost:5000 ps:当你请求http:&#x2F;&#x2F;localhost:5000产生跨域(本身在 3000 端口)时,添加此代码, 之后你请求时用http:&#x2F;&#x2F;localhost:3000进行请求,当">
<meta property="og:type" content="article">
<meta property="og:title" content="React 配置代理">
<meta property="og:url" content="https://ma_jinkun.gitee.io/2021/12/30/react/react-proxy/index.html">
<meta property="og:site_name" content="小马の博客">
<meta property="og:description" content="React ajax axios 配置代理1、React 中配置代理(proxy)  简单代理:在 package.json 中追加如下配置 :&quot;proxy&quot;:http:&#x2F;&#x2F;localhost:5000 ps:当你请求http:&#x2F;&#x2F;localhost:5000产生跨域(本身在 3000 端口)时,添加此代码, 之后你请求时用http:&#x2F;&#x2F;localhost:3000进行请求,当">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://s2.loli.net/2021/12/28/Al7LXsN2hYH4icO.jpg">
<meta property="article:published_time" content="2021-12-30T02:14:13.000Z">
<meta property="article:modified_time" content="2022-01-05T03:19:05.550Z">
<meta property="article:author" content="Kun Kun">
<meta property="article:tag" content="React 配置代理">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://s2.loli.net/2021/12/28/Al7LXsN2hYH4icO.jpg"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://ma_jinkun.gitee.io/2021/12/30/react/react-proxy/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: true,
    post: true
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'React 配置代理',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-01-05 11:19:05'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><!-- hexo injector head_end start --><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-card-history/baiduhistory/css/main.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-swiper/swiper/swiper.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-swiper/swiper/swiperstyle.css"><!-- hexo injector head_end end --><meta name="generator" content="Hexo 5.4.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://s2.loli.net/2021/12/28/uBIDZUEFrpsCvH7.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">8</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">7</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">6</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fa fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音樂</span></a></div><div class="menus_item"><a class="site-page" href="/contact/"><i class="fa-fw far fa-comments"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fa fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fa fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://s2.loli.net/2021/12/30/OjiswQgfeP3Jm4S.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">小马の博客</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fa fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/music/"><i class="fa-fw fas fa-music"></i><span> 音樂</span></a></div><div class="menus_item"><a class="site-page" href="/contact/"><i class="fa-fw far fa-comments"></i><span> 留言板</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fa fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fa fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">React 配置代理</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-12-30T02:14:13.000Z" title="发表于 2021-12-30 10:14:13">2021-12-30</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-01-05T03:19:05.550Z" title="更新于 2022-01-05 11:19:05">2022-01-05</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/React%E5%AD%A6%E4%B9%A0-%E9%85%8D%E7%BD%AE%E4%BB%A3%E7%90%86/">React学习  配置代理</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="React 配置代理"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="React-ajax-axios-配置代理"><a href="#React-ajax-axios-配置代理" class="headerlink" title="React ajax axios 配置代理"></a>React ajax axios 配置代理</h1><h3 id="1、React-中配置代理-proxy"><a href="#1、React-中配置代理-proxy" class="headerlink" title="1、React 中配置代理(proxy)"></a>1、React 中配置代理(<code>proxy</code>)</h3><blockquote>
<ol>
<li><code>简单代理</code>:在 package.json 中追加如下配置 :<code>&quot;proxy&quot;:http://localhost:5000</code><ul>
<li>ps:当你请求<code>http://localhost:5000</code>产生跨域(本身在 3000 端口)时,添加此代码, 之后你请求时用<code>http://localhost:3000</code>进行请求,当其在<code>3000</code>端口中找不到资源时将会自动转发至<code>5000</code>端口进行请求,不产生跨域问题</li>
<li>优点：配置简单，前端请求资源时可以不加任何前缀。</li>
<li>缺点：不能配置多个代理</li>
<li>工作方式：上述方式配置代理，当请求了 3000 不存在的资源时，那么该请求会转发给 5000 （优先匹配前端资源）</li>
</ul>
</li>
<li>方法二: 在 src 下创建配置文件：<code>src/setupProxy.js</code><ul>
<li>ps:必须是这个文件名,react 项目运行的时候会自动查找这个文件,并将其加入 webpack 的配置中,所以当你修改此文件后,你需要重新启动项目</li>
<li>优点：可以配置多个代理，可以灵活的控制请求是否走代理。</li>
<li>缺点：配置繁琐，前端请求资源时必须加前缀。</li>
</ul>
</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//代码示例</span></span><br><span class="line"><span class="keyword">const</span> proxy = <span class="built_in">require</span>(<span class="string">&quot;http-proxy-middleware&quot;</span>);</span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="keyword">function</span> (<span class="params">app</span>) </span>&#123;</span><br><span class="line">  app.use(</span><br><span class="line">    proxy(<span class="string">&quot;/api1&quot;</span>, &#123;</span><br><span class="line">      <span class="comment">//api1是需要转发的请求(所有带有/api1前缀的请求都会转发给5000)</span></span><br><span class="line">      <span class="attr">target</span>: <span class="string">&quot;http://localhost:5000&quot;</span>, <span class="comment">//配置转发目标地址(能返回数据的服务器地址)</span></span><br><span class="line">      <span class="attr">changeOrigin</span>: <span class="literal">true</span>, <span class="comment">//控制服务器接收到的请求头中host字段的值</span></span><br><span class="line">      <span class="comment">/*</span></span><br><span class="line"><span class="comment">       	changeOrigin设置为true时，服务器收到的请求头中的host为：localhost:5000</span></span><br><span class="line"><span class="comment">       	changeOrigin设置为false时，服务器收到的请求头中的host为：localhost:3000</span></span><br><span class="line"><span class="comment">       	changeOrigin默认值为false，但我们一般将changeOrigin值设为true</span></span><br><span class="line"><span class="comment">       */</span></span><br><span class="line">      <span class="attr">pathRewrite</span>: &#123; <span class="string">&quot;^/api1&quot;</span>: <span class="string">&quot;&quot;</span> &#125;, <span class="comment">//去除请求前缀，保证交给后台服务器的是正常请求地址(必须配置)</span></span><br><span class="line">    &#125;),</span><br><span class="line">    proxy(<span class="string">&quot;/api2&quot;</span>, &#123;</span><br><span class="line">      <span class="attr">target</span>: <span class="string">&quot;http://localhost:5001&quot;</span>,</span><br><span class="line">      <span class="attr">changeOrigin</span>: <span class="literal">true</span>,</span><br><span class="line">      <span class="attr">pathRewrite</span>: &#123; <span class="string">&quot;^/api2&quot;</span>: <span class="string">&quot;&quot;</span> &#125;,</span><br><span class="line">    &#125;)</span><br><span class="line">  );</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
</blockquote>
<h3 id="注意：记录一个坑"><a href="#注意：记录一个坑" class="headerlink" title="注意：记录一个坑"></a>注意：记录一个坑</h3><blockquote>
<p>在你配置好 proxy 会 如果出现了运行报错 还在页面打不开的这种情况 有可能是<br>setupProxy.js 文件导致 会打不开网页 这个问题是版本问题，http-proxy-middleware 的版本如果 1.x 以上，</p>
<p>// 解决: 把里面的 proxy 换成 createProxyMiddleware</p>
</blockquote>
<h4 id="Ⅱ-消息订阅与发布机制-—-gt-工具库-PubSubJS"><a href="#Ⅱ-消息订阅与发布机制-—-gt-工具库-PubSubJS" class="headerlink" title="Ⅱ-消息订阅与发布机制 —&gt; 工具库: PubSubJS"></a>Ⅱ-消息订阅与发布机制 —&gt; 工具库: PubSubJS</h4><blockquote>
<p>1.先订阅，再发布（理解：有一种隔空对话的感觉）</p>
<p>2.适用于任意组件间通信</p>
<p>3.要在组件的 componentWillUnmount 中取消订阅</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//下载: npm install pubsub-js --save</span></span><br><span class="line"><span class="comment">//使用举例</span></span><br><span class="line"><span class="number">1</span>)	<span class="keyword">import</span> PubSub <span class="keyword">from</span> <span class="string">&#x27;pubsub-js&#x27;</span> <span class="comment">//引入</span></span><br><span class="line"><span class="number">2</span>)	PubSub.subscribe(<span class="string">&#x27;delete&#x27;</span>, <span class="function"><span class="keyword">function</span>(<span class="params">data</span>)</span>&#123; &#125;); <span class="comment">//订阅</span></span><br><span class="line"><span class="number">3</span>)	PubSub.publish(<span class="string">&#x27;delete&#x27;</span>, data) <span class="comment">//发布消息</span></span><br><span class="line"><span class="comment">//*------------------------------使用----------------------------------------------------</span></span><br><span class="line">	<span class="function"><span class="title">componentDidMount</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">		<span class="built_in">this</span>.token = PubSub.subscribe(<span class="string">&#x27;atguigu&#x27;</span>,<span class="function">(<span class="params">_,stateObj</span>)=&gt;</span>&#123;</span><br><span class="line">			<span class="built_in">this</span>.setState(stateObj)</span><br><span class="line">		&#125;)</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="function"><span class="title">componentWillUnmount</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">		PubSub.unsubscribe(<span class="built_in">this</span>.token)</span><br><span class="line">	&#125;</span><br><span class="line"><span class="comment">//----------------------------------使用---------------------------------------------------</span></span><br><span class="line">		<span class="comment">//发送请求前通知List更新状态</span></span><br><span class="line">		PubSub.publish(<span class="string">&#x27;atguigu&#x27;</span>,&#123;<span class="attr">isFirst</span>:<span class="literal">false</span>,<span class="attr">isLoading</span>:<span class="literal">true</span>&#125;)</span><br><span class="line">		<span class="comment">//发送网络请求---使用fetch发送（优化）</span></span><br><span class="line">		<span class="keyword">try</span> &#123;</span><br><span class="line">			<span class="keyword">const</span> response= <span class="keyword">await</span> fetch(<span class="string">`/api1/search/users2?q=<span class="subst">$&#123;keyWord&#125;</span>`</span>)</span><br><span class="line">			<span class="keyword">const</span> data = <span class="keyword">await</span> response.json()</span><br><span class="line">			<span class="built_in">console</span>.log(data);</span><br><span class="line">			PubSub.publish(<span class="string">&#x27;atguigu&#x27;</span>,&#123;<span class="attr">isLoading</span>:<span class="literal">false</span>,<span class="attr">users</span>:data.items&#125;)</span><br><span class="line">		&#125; <span class="keyword">catch</span> (error) &#123;</span><br><span class="line">			<span class="built_in">console</span>.log(<span class="string">&#x27;请求出错&#x27;</span>,error);</span><br><span class="line">			PubSub.publish(<span class="string">&#x27;atguigu&#x27;</span>,&#123;<span class="attr">isLoading</span>:<span class="literal">false</span>,<span class="attr">err</span>:error.message&#125;)</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br></pre></td></tr></table></figure>
</blockquote>
<h4 id="Ⅲ-消息订阅与发布机制-—-gt-工具库-mitt"><a href="#Ⅲ-消息订阅与发布机制-—-gt-工具库-mitt" class="headerlink" title="Ⅲ-消息订阅与发布机制 —&gt; 工具库: mitt"></a>Ⅲ-消息订阅与发布机制 —&gt; 工具库: mitt</h4><blockquote>
<p>这是本人后来在 githyb 中找到并应用在项目代码中的,此工具库代码量特别少,可以阅读源码,会有很大好处</p>
<p>此方法用的是[<code>mitt</code>]实现,其实本质上就是注册一个全局变量进行监听 –&gt; <a target="_blank" rel="noopener" href="https://github.com/developit/mitt">mitt 源码地址</a></p>
<p>可以自己实现,此处因为人家写的不错了,就以此作为例子</p>
<ol>
<li> 安装或者直接复制使用</li>
</ol>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">npm install --save mitt</span><br></pre></td></tr></table></figure>

<ol start="2">
<li> 使用示例</li>
</ol>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">//</span><br><span class="line">-------------- 首先要定义一个公用全局变量  --------------------------</span><br><span class="line">//文件 utils/index.ts</span><br><span class="line">import mitt from &#x27;./mitt&#x27;;</span><br><span class="line">//此处声明,将其变为全局变量</span><br><span class="line">const eventBus = mitt();</span><br><span class="line">export &#123; eventBus &#125;</span><br><span class="line">---------------- 发送值的组件(要修改别人的组件)  ---------------------</span><br><span class="line">//导入共有变量</span><br><span class="line">import &#123; eventBus &#125; from &#x27;~/utils&#x27;;</span><br><span class="line">  &lt;a</span><br><span class="line">  onClick=&#123;() =&gt; &#123;</span><br><span class="line">	//延迟发送是本人此之前有一个跳转动作,跳转到接收方组件</span><br><span class="line">    // 防止修改了值的时候但是接收组件未注册  正常情况直接发送即可</span><br><span class="line">    //setTimeout(() =&gt; &#123;</span><br><span class="line">    // eventBus.emit(&#x27;foo&#x27;, data);</span><br><span class="line">    //&#125;, 100);</span><br><span class="line">    eventBus.emit(&#x27;foo&#x27;, data);</span><br><span class="line">   &#125;&#125;</span><br><span class="line">  /&gt;;</span><br><span class="line"></span><br><span class="line">------------------ 接受方组件(接受发送方的组件)  -------------------------------------</span><br><span class="line"></span><br><span class="line">const Search: FC&lt;IProps&gt; = (props) =&gt; &#123;</span><br><span class="line">  useEffect(() =&gt; &#123;</span><br><span class="line">    //替换为mitt写法,此时已经接收到了</span><br><span class="line">    eventBus.on(&#x27;foo&#x27;, (searchParams) =&gt; &#123;console.log(&#x27;接受到值了&#x27;,searchParams) &#125;</span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;, []);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ol start="3">
<li> mitt 源码</li>
</ol>
<figure class="highlight ts"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">type</span> EventType = <span class="built_in">string</span> | symbol;</span><br><span class="line"></span><br><span class="line"><span class="comment">// An event handler can take an optional event argument</span></span><br><span class="line"><span class="comment">// and should not return a value</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">type</span> Handler&lt;T = unknown&gt; = <span class="function">(<span class="params">event: T</span>) =&gt;</span> <span class="built_in">void</span>;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">type</span> WildcardHandler&lt;T = Record&lt;<span class="built_in">string</span>, unknown&gt;&gt; = <span class="function">(<span class="params"></span></span></span><br><span class="line"><span class="params"><span class="function">  <span class="keyword">type</span>: keyof T,</span></span></span><br><span class="line"><span class="params"><span class="function">  event: T[keyof T]</span></span></span><br><span class="line"><span class="params"><span class="function"></span>) =&gt;</span> <span class="built_in">void</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// An array of all currently registered event handlers for a type</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">type</span> EventHandlerList&lt;T = unknown&gt; = <span class="built_in">Array</span>&lt;Handler&lt;T&gt;&gt;;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">type</span> WildCardEventHandlerList&lt;T = Record&lt;<span class="built_in">string</span>, unknown&gt;&gt; = <span class="built_in">Array</span>&lt;</span><br><span class="line">  WildcardHandler&lt;T&gt;</span><br><span class="line">&gt;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// A map of event types and their corresponding event handlers.</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">type</span> EventHandlerMap&lt;Events <span class="keyword">extends</span> Record&lt;EventType, unknown&gt;&gt; = <span class="built_in">Map</span>&lt;</span><br><span class="line">  keyof Events | <span class="string">&quot;*&quot;</span>,</span><br><span class="line">  EventHandlerList&lt;Events[keyof Events]&gt; | WildCardEventHandlerList&lt;Events&gt;</span><br><span class="line">&gt;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">interface</span> Emitter&lt;Events <span class="keyword">extends</span> Record&lt;EventType, unknown&gt;&gt; &#123;</span><br><span class="line">  <span class="attr">all</span>: EventHandlerMap&lt;Events&gt;;</span><br><span class="line"></span><br><span class="line">  on: (&lt;Key extends keyof Events&gt;(</span><br><span class="line">    type: Key,</span><br><span class="line">    handler: Handler&lt;Events[Key]&gt;</span><br><span class="line">  ) =&gt; void) &amp;</span><br><span class="line">    ((type: &quot;*&quot;, handler: WildcardHandler&lt;Events&gt;) =&gt; void);</span><br><span class="line"></span><br><span class="line">  off: (&lt;Key extends keyof Events&gt;(</span><br><span class="line">    type: Key,</span><br><span class="line">    handler?: Handler&lt;Events[Key]&gt;</span><br><span class="line">  ) =&gt; void) &amp;</span><br><span class="line">    ((type: &quot;*&quot;, handler: WildcardHandler&lt;Events&gt;) =&gt; void);</span><br><span class="line"></span><br><span class="line">  emit: (&lt;Key extends keyof Events&gt;(type: Key, event: Events[Key]) =&gt; void) &amp;</span><br><span class="line">    (&lt;Key extends keyof Events&gt;(</span><br><span class="line">      type: undefined extends Events[Key] ? Key : never</span><br><span class="line">    ) =&gt; void);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * Mitt: Tiny (~200b) functional event emitter / pubsub.</span><br><span class="line"> * @name mitt</span><br><span class="line"> * @returns &#123;Mitt&#125;</span><br><span class="line"> */</span><br><span class="line">export default function mitt&lt;Events extends Record&lt;EventType, unknown&gt;&gt;(</span><br><span class="line">  all?: EventHandlerMap&lt;Events&gt;</span><br><span class="line">): Emitter&lt;Events&gt; &#123;</span><br><span class="line">  type GenericEventHandler =</span><br><span class="line">    | Handler&lt;Events[keyof Events]&gt;</span><br><span class="line">    | WildcardHandler&lt;Events&gt;;</span><br><span class="line">  all = all || new Map();</span><br><span class="line"></span><br><span class="line">  return &#123;</span><br><span class="line">    /**</span><br><span class="line">     * A Map of event names to registered handler functions.</span><br><span class="line">     */</span><br><span class="line">    all,</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * Register an event handler for the given type.</span><br><span class="line">     * @param &#123;string|symbol&#125; type Type of event to listen for, or `&#x27;*&#x27;` for all events</span><br><span class="line">     * @param &#123;Function&#125; handler Function to call in response to given event</span><br><span class="line">     * @memberOf mitt</span><br><span class="line">     */</span><br><span class="line">    on&lt;Key extends keyof Events&gt;(type: Key, handler: GenericEventHandler) &#123;</span><br><span class="line">      const handlers: Array&lt;GenericEventHandler&gt; | undefined = all!.get(type);</span><br><span class="line">      if (handlers) &#123;</span><br><span class="line">        handlers.push(handler);</span><br><span class="line">      &#125; else &#123;</span><br><span class="line">        all!.set(type, [handler] as EventHandlerList&lt;Events[keyof Events]&gt;);</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;,</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * Remove an event handler for the given type.</span><br><span class="line">     * If `handler` is omitted, all handlers of the given type are removed.</span><br><span class="line">     * @param &#123;string|symbol&#125; type Type of event to unregister `handler` from, or `&#x27;*&#x27;`</span><br><span class="line">     * @param &#123;Function&#125; [handler] Handler function to remove</span><br><span class="line">     * @memberOf mitt</span><br><span class="line">     */</span><br><span class="line">    off&lt;Key extends keyof Events&gt;(type: Key, handler?: GenericEventHandler) &#123;</span><br><span class="line">      const handlers: Array&lt;GenericEventHandler&gt; | undefined = all!.get(type);</span><br><span class="line">      if (handlers) &#123;</span><br><span class="line">        if (handler) &#123;</span><br><span class="line">          handlers.splice(handlers.indexOf(handler) &gt;&gt;&gt; 0, 1);</span><br><span class="line">        &#125; else &#123;</span><br><span class="line">          all!.set(type, []);</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;,</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * Invoke all handlers for the given type.</span><br><span class="line">     * If present, `&#x27;*&#x27;` handlers are invoked after type-matched handlers.</span><br><span class="line">     *</span><br><span class="line">     * Note: Manually firing &#x27;*&#x27; handlers is not supported.</span><br><span class="line">     *</span><br><span class="line">     * @param &#123;string|symbol&#125; type The event type to invoke</span><br><span class="line">     * @param &#123;Any&#125; [evt] Any value (object is recommended and powerful), passed to each handler</span><br><span class="line">     * @memberOf mitt</span><br><span class="line">     */</span><br><span class="line">    emit&lt;Key extends keyof Events&gt;(type: Key, evt?: Events[Key]) &#123;</span><br><span class="line">      let handlers = all!.get(type);</span><br><span class="line">      if (handlers) &#123;</span><br><span class="line">        (handlers as EventHandlerList&lt;Events[keyof Events]&gt;)</span><br><span class="line">          .slice()</span><br><span class="line">          .map((handler) =&gt; &#123;</span><br><span class="line">            handler(evt!);</span><br><span class="line">          &#125;);</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      handlers = all!.get(&quot;*&quot;);</span><br><span class="line">      if (handlers) &#123;</span><br><span class="line">        (handlers as WildCardEventHandlerList&lt;Events&gt;)</span><br><span class="line">          .slice()</span><br><span class="line">          .map((handler) =&gt; &#123;</span><br><span class="line">            handler(type, evt!);</span><br><span class="line">          &#125;);</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</blockquote>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">Kun Kun</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://ma_jinkun.gitee.io/2021/12/30/react/react-proxy/">https://ma_jinkun.gitee.io/2021/12/30/react/react-proxy/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://ma_jinkun.gitee.io" target="_blank">小马の博客</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/React-%E9%85%8D%E7%BD%AE%E4%BB%A3%E7%90%86/">React 配置代理</a></div><div class="post_share"><div class="social-share" data-image="https://s2.loli.net/2021/12/28/Al7LXsN2hYH4icO.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2022/01/04/react-ly/"><img class="prev-cover" src="[&quot;https://image-tupss.test.upcdn.net/baner.png&quot;]" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">React路由</div></div></a></div><div class="next-post pull-right"><a href="/2021/12/30/react/react-cycle/"><img class="next-cover" src="https://s2.loli.net/2021/12/28/Al7LXsN2hYH4icO.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">React(生命周期,脚手架)</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://s2.loli.net/2021/12/28/uBIDZUEFrpsCvH7.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">Kun Kun</div><div class="author-info__description">嘘,别看,眼睛会疼！</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">8</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">7</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">6</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/xxxxxx"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/xxxxx" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="mailto:2583931485@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a><a class="social-icon" href="qq:2583931485" target="_blank" title="QQ"><i class="fab fa-qq"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">基于Hexo 主题butterfly 开发的</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#React-ajax-axios-%E9%85%8D%E7%BD%AE%E4%BB%A3%E7%90%86"><span class="toc-number">1.</span> <span class="toc-text">React ajax axios 配置代理</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81React-%E4%B8%AD%E9%85%8D%E7%BD%AE%E4%BB%A3%E7%90%86-proxy"><span class="toc-number">1.0.1.</span> <span class="toc-text">1、React 中配置代理(proxy)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B3%A8%E6%84%8F%EF%BC%9A%E8%AE%B0%E5%BD%95%E4%B8%80%E4%B8%AA%E5%9D%91"><span class="toc-number">1.0.2.</span> <span class="toc-text">注意：记录一个坑</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E2%85%A1-%E6%B6%88%E6%81%AF%E8%AE%A2%E9%98%85%E4%B8%8E%E5%8F%91%E5%B8%83%E6%9C%BA%E5%88%B6-%E2%80%94-gt-%E5%B7%A5%E5%85%B7%E5%BA%93-PubSubJS"><span class="toc-number">1.0.2.1.</span> <span class="toc-text">Ⅱ-消息订阅与发布机制 —&gt; 工具库: PubSubJS</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E2%85%A2-%E6%B6%88%E6%81%AF%E8%AE%A2%E9%98%85%E4%B8%8E%E5%8F%91%E5%B8%83%E6%9C%BA%E5%88%B6-%E2%80%94-gt-%E5%B7%A5%E5%85%B7%E5%BA%93-mitt"><span class="toc-number">1.0.2.2.</span> <span class="toc-text">Ⅲ-消息订阅与发布机制 —&gt; 工具库: mitt</span></a></li></ol></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2022/01/04/react-ly/" title="React路由"><img src="[&quot;https://image-tupss.test.upcdn.net/baner.png&quot;]" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="React路由"/></a><div class="content"><a class="title" href="/2022/01/04/react-ly/" title="React路由">React路由</a><time datetime="2022-01-04T06:28:11.000Z" title="发表于 2022-01-04 14:28:11">2022-01-04</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/12/30/react/react-proxy/" title="React 配置代理"><img src="https://s2.loli.net/2021/12/28/Al7LXsN2hYH4icO.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="React 配置代理"/></a><div class="content"><a class="title" href="/2021/12/30/react/react-proxy/" title="React 配置代理">React 配置代理</a><time datetime="2021-12-30T02:14:13.000Z" title="发表于 2021-12-30 10:14:13">2021-12-30</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/12/30/react/react-cycle/" title="React(生命周期,脚手架)"><img src="https://s2.loli.net/2021/12/28/Al7LXsN2hYH4icO.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="React(生命周期,脚手架)"/></a><div class="content"><a class="title" href="/2021/12/30/react/react-cycle/" title="React(生命周期,脚手架)">React(生命周期,脚手架)</a><time datetime="2021-12-30T01:58:45.000Z" title="发表于 2021-12-30 09:58:45">2021-12-30</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/12/30/react/react-two/" title="React第二天(组件三大属性)"><img src="[&quot;https://s2.loli.net/2022/01/05/ezb8QtqIdmLEShW.jpg&quot;]" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="React第二天(组件三大属性)"/></a><div class="content"><a class="title" href="/2021/12/30/react/react-two/" title="React第二天(组件三大属性)">React第二天(组件三大属性)</a><time datetime="2021-12-30T01:49:08.000Z" title="发表于 2021-12-30 09:49:08">2021-12-30</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/12/29/react/React-one/" title="React学习"><img src="https://s2.loli.net/2021/12/28/Al7LXsN2hYH4icO.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="React学习"/></a><div class="content"><a class="title" href="/2021/12/29/react/React-one/" title="React学习">React学习</a><time datetime="2021-12-29T09:24:15.000Z" title="发表于 2021-12-29 17:24:15">2021-12-29</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By Kun Kun</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><div class="js-pjax"></div><script defer="defer" id="ribbon" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-ribbon.min.js" size="150" alpha="0.6" zIndex="-1" mobile="false" data-click="false"></script><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-heart.min.js" async="async" mobile="true"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><!-- hexo injector body_end start --><script data-pjax>function history_calendar_injector_config(){
                var parent_div_git = document.getElementsByClassName('sticky_layout')[0];
                var item_html = '<div class="card-widget card-history"><div class="card-content"><div class="item-headline"><i class="fas fa-clock fa-spin"></i><span>那年今日</span></div><div id="history-baidu" style="height: 100px;overflow: hidden"><div class="history_swiper-container" id="history-container" style="width: 100%;height: 100%"><div class="swiper-wrapper" id="history_container_wrapper" style="height:20px"></div></div></div></div>';
                console.log('已挂载history_calendar')
                // parent_div_git.innerHTML=item_html+parent_div_git.innerHTML // 无报错，但不影响使用(支持pjax跳转)
                parent_div_git.insertAdjacentHTML("afterbegin",item_html) // 有报错，但不影响使用(支持pjax跳转)
            }if( document.getElementsByClassName('sticky_layout')[0] && (location.pathname ==='all'|| 'all' ==='all')){

            history_calendar_injector_config()
        } </script><script data-pjax src="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-card-history/baiduhistory/js/main.js"></script> <script data-pjax>if(document.getElementById('recent-posts') && location.pathname =='/'){
    
    var parent = document.getElementById('recent-posts');
    var child = '<div class="recent-post-item" style="height: auto;width: 100%"><div class="blog-slider swiper-container-fade swiper-container-horizontal" id="swiper_container"><div class="blog-slider__wrp swiper-wrapper" style="transition-duration: 0ms"><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms"><div class="blog-slider__img"><img src="https://s2.loli.net/2021/12/28/Al7LXsN2hYH4icO.jpg" alt="https://s2.loli.net/2021/12/28/Al7LXsN2hYH4icO.jpg"/></div><div class="blog-slider__content"><span class="blog-slider__code">2021-12-28</span><a class="blog-slider__title" href="2021/12/28/My-First-Artical/">第一次测试文档</a><div class="blog-slider__text">简单好用的 hexo 博客文章置顶插件！</div><a class="blog-slider__button" href="2021/12/28/My-First-Artical/">详情</a></div></div><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms"><div class="blog-slider__img"><img src="https://image-tupss.test.upcdn.net/baner.png" alt="https://image-tupss.test.upcdn.net/baner.png"/></div><div class="blog-slider__content"><span class="blog-slider__code">2022-01-04</span><a class="blog-slider__title" href="2022/01/04/react-ly/">React路由</a><div class="blog-slider__text">React路由！</div><a class="blog-slider__button" href="2022/01/04/react-ly/">详情</a></div></div><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms"><div class="blog-slider__img"><img src="https://s2.loli.net/2021/12/28/Al7LXsN2hYH4icO.jpg" alt="https://s2.loli.net/2021/12/28/Al7LXsN2hYH4icO.jpg"/></div><div class="blog-slider__content"><span class="blog-slider__code">2021-12-30</span><a class="blog-slider__title" href="2021/12/30/react/react-cycle/">React(生命周期,脚手架)</a><div class="blog-slider__text">生命周期 脚手架！</div><a class="blog-slider__button" href="2021/12/30/react/react-cycle/">详情</a></div></div><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms"><div class="blog-slider__img"><img src="https://s2.loli.net/2021/12/28/Al7LXsN2hYH4icO.jpg" alt="https://s2.loli.net/2021/12/28/Al7LXsN2hYH4icO.jpg"/></div><div class="blog-slider__content"><span class="blog-slider__code">2021-12-30</span><a class="blog-slider__title" href="2021/12/30/react/react-proxy/">React 配置代理</a><div class="blog-slider__text">配置代理！</div><a class="blog-slider__button" href="2021/12/30/react/react-proxy/">详情</a></div></div><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms"><div class="blog-slider__img"><img src="https://s2.loli.net/2022/01/05/ezb8QtqIdmLEShW.jpg" alt="https://s2.loli.net/2022/01/05/ezb8QtqIdmLEShW.jpg"/></div><div class="blog-slider__content"><span class="blog-slider__code">2021-12-30</span><a class="blog-slider__title" href="2021/12/30/react/react-two/">React第二天(组件三大属性)</a><div class="blog-slider__text">第二天！</div><a class="blog-slider__button" href="2021/12/30/react/react-two/">详情</a></div></div><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms"><div class="blog-slider__img"><img src="https://s2.loli.net/2021/12/28/Al7LXsN2hYH4icO.jpg" alt="https://s2.loli.net/2021/12/28/Al7LXsN2hYH4icO.jpg"/></div><div class="blog-slider__content"><span class="blog-slider__code">2021-12-29</span><a class="blog-slider__title" href="2021/12/29/react/React-one/">React学习</a><div class="blog-slider__text">第一天！</div><a class="blog-slider__button" href="2021/12/29/react/React-one/">详情</a></div></div></div><div class="blog-slider__pagination swiper-pagination-clickable swiper-pagination-bullets"></div></div></div>';
    console.log('已挂载swiper')
    parent.insertAdjacentHTML("afterbegin",child)}
     </script>
<script data-pjax src="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-swiper/swiper/swiper.min.js"></script>
<script data-pjax src="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-swiper@0.18/swiper/swiperindex.js"></script>
<style></style><!-- hexo injector body_end end --></body></html>